package com.skcc.cop.mrtaxi.biz;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.skcc.cop.mrtaxi.BaseActivity;
import com.skcc.cop.mrtaxi.PreferenceManager;
import com.skcc.cop.mrtaxi.R;
import com.skcc.cop.mrtaxi.util.Dialogs;
import com.skcc.cop.mrtaxi.util.Dialogs.DialogListener;
import com.skcc.cop.mrtaxi.view.TitleBar;
import com.skcc.cop.mrtaxi.view.TitleBar.OnTitleBarEventListener;

public class UserAgreementActivity extends BaseActivity  implements OnTitleBarEventListener {

	// ===========================================================
	// Constants
	// ===========================================================
	private static final String TAG = UserAgreementActivity.class.getSimpleName();

	
	// ===========================================================
    // Fields
    // ===========================================================
	private PreferenceManager preference;
	
	private TitleBar mTitleBarView;
	
	private View showUserAgreement1;
	private View showUserAgreement2;
	private View showUserAgreement3;
	
	private View userAgreementButton1;
	private View userAgreementButton2;
	private View userAgreementButton3;
	
	private View userAgreementCheckImage1;
	private View userAgreementCheckImage2;
	private View userAgreementCheckImage3;
	
	private boolean userAgreementFlag1 = false;
	private boolean userAgreementFlag2 = false;
	private boolean userAgreementFlag3 = false;

	
    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_agreement);

		preference = application.getPreferenceManager();
		
		mTitleBarView = (TitleBar) this.findViewById(R.id.titlebar_view);
		mTitleBarView.setEventListener(this);
		mTitleBarView.setTitle(R.string.user_agreement);

		showUserAgreement1 = this.findViewById(R.id.user_agreement1_view_layout);
		userAgreementButton1 = this.findViewById(R.id.user_agreement1_check_layout);
		userAgreementCheckImage1 = (ImageView) this.findViewById(R.id.user_agreement1_check_imageview);
		showUserAgreement1.setOnClickListener(clickListener);
		userAgreementButton1.setOnClickListener(clickListener);
		
		showUserAgreement2 = this.findViewById(R.id.user_agreement2_view_layout);
		userAgreementButton2 = this.findViewById(R.id.user_agreement2_check_layout);
		userAgreementCheckImage2 = (ImageView) this.findViewById(R.id.user_agreement2_check_imageview);
		showUserAgreement2.setOnClickListener(clickListener);
		userAgreementButton2.setOnClickListener(clickListener);
		
		
		showUserAgreement3 = this.findViewById(R.id.user_agreement3_view_layout);
		userAgreementButton3 = this.findViewById(R.id.user_agreement3_check_layout);
		userAgreementCheckImage3 = (ImageView) this.findViewById(R.id.user_agreement3_check_imageview);
		showUserAgreement3.setOnClickListener(clickListener);
		userAgreementButton3.setOnClickListener(clickListener);
		
	}
	
	@Override
	public void onBackClicked() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAddClicked() {
		// TODO Auto-generated method stub
		
	}
	
	
	// ===========================================================
	// Methods
	// ===========================================================
	private OnClickListener clickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(v.getId() == R.id.user_agreement1_view_layout) {
				// TODO: 서비스 이용약관 화면으로 이동
				Log.i(TAG, ">>>>>>>>>>>> 서비스이용약관");
				Intent intent = new Intent(UserAgreementActivity.this,
						TermsConditionsOfUseActivity.class);
				startActivity(intent);
			} else if(v.getId() == R.id.user_agreement1_check_layout) {
				userAgreementFlag1 = !userAgreementFlag1;
				
				if (userAgreementFlag1) {
					userAgreementCheckImage1.setBackgroundResource(R.drawable.agree_checked);
					userAgreementButton1.setBackgroundResource(R.drawable.btn_agree_checked_bg);
				} else {
					userAgreementCheckImage1.setBackgroundResource(R.drawable.agree);
					userAgreementButton1.setBackgroundResource(R.drawable.btn_agree_bg);
				}
				
			} else if(v.getId() == R.id.user_agreement2_view_layout) {
				// TODO: 개인정보 수집 및 이용에 대한 동의 화면으로 이동
				Log.i(TAG, ">>>>>>>>>>>> 개인정보 수집 및 이용에 대한 동의");
				Intent intent = new Intent(UserAgreementActivity.this,
						PrivacyPolicyActivity.class);
				startActivity(intent);
			} else if(v.getId() == R.id.user_agreement2_check_layout) {
				userAgreementFlag2 = !userAgreementFlag2;
				
				if (userAgreementFlag2) {
					userAgreementCheckImage2.setBackgroundResource(R.drawable.agree_checked);
					userAgreementButton2.setBackgroundResource(R.drawable.btn_agree_checked_bg);
				} else {
					userAgreementCheckImage2.setBackgroundResource(R.drawable.agree);
					userAgreementButton2.setBackgroundResource(R.drawable.btn_agree_bg);
				}
				
			} else if(v.getId() == R.id.user_agreement3_view_layout) {
				// TODO: 서비스 유료 이용약관 화면으로 이동
				
			} else if(v.getId() == R.id.user_agreement3_check_layout) {
				userAgreementFlag3 = !userAgreementFlag3;
				
				if (userAgreementFlag3) {
					userAgreementCheckImage3.setBackgroundResource(R.drawable.agree_checked);
					userAgreementButton3.setBackgroundResource(R.drawable.btn_agree_checked_bg);
				} else {
					userAgreementCheckImage3.setBackgroundResource(R.drawable.agree);
					userAgreementButton3.setBackgroundResource(R.drawable.btn_agree_bg);
				}
				
				if (userAgreementFlag1 && userAgreementFlag2 && userAgreementFlag3) {
					UserAgreementActivity.this.setResult(RESULT_OK);
					finish();
				} else {
					Dialogs.showDialog(UserAgreementActivity.this, getString(R.string.user_agreement_notice), dialogListener);
				}
			} 
			
			return;
		}
	};
	
	
	private DialogListener dialogListener = new DialogListener() {

		@Override
		public void onClickedOk() {
			Dialogs.dismiss();
			
			userAgreementFlag3 = false;
			userAgreementCheckImage3.setBackgroundResource(R.drawable.agree);
			userAgreementButton3.setBackgroundResource(R.drawable.btn_agree_bg);
		}

		@Override
		public void onClickedCancel() {
			// TODO Auto-generated method stub
		}
	};


}
